#ifndef __SAMBA_MANAGER__H__
#define __SAMBA_MANAGER__H__

#include <QTextStream>
#include <QFileInfo>
#include <QDir>

/**
#VERSION 2
path=/home/zinface/Pictures
comment=
usershare_acl=S-1-1-0:F
guest_ok=n
sharename=图片
 */
class SambaShare {
public:
    SambaShare(QString content) {
        m_content = content;
    }

    QString content() {
        return m_content;
    }

    QString version() {
        return get("version");
    }
    QString path() {
        return get("path");
    }
    QString comment() {
        return get("comment");
    }
    QString usershare_acl() {
        return get("usershare_acl");
    }
    QString guest_ok() {
        return get("guest_ok");
    }
    QString sharename() {
        return get("sharename");
    }


    static SambaShare createShare(QString share_name, QString share_path) {
        QString share_template = "#VERSION 2\n"
                          "path=%2\n"
                          "comment=\n"
                          "usershare_acl=S-1-1-0:F\n"
                          "guest_ok=n\n"
                          "sharename=%1";
        QString share_content = QString(share_template).arg(share_name).arg(share_path);
        return SambaShare(share_content);
    }

private:
    QString get(QString prefix) {
        for (auto &var : m_content.split('\n', Qt::SkipEmptyParts))
        {
            if (prefix == "version") {
                if (var.startsWith("#VERSION")) {
                    auto var_splits = var.split(' ');
                    if (var_splits.count() == 2) {
                        return var_splits.at(1);
                    }    
                }
            }

            if (var.startsWith(prefix)) {
                auto var_splits = var.split('=');
                if (var_splits.count() == 2) {
                    return var_splits.at(1);
                }
            }
        }
        return QString();
    }

private:
    QString m_content;
};


const QString var_lib_samba = QString("/var/lib/samba") +  QDir::separator();
const QString var_lib_samba_usersahres = var_lib_samba +  "usershares" +  QDir::separator();

class SambaManager {
public:
    SambaManager() {
        reload();
    }

    void reload() {
        QDir smb_dir(var_lib_samba);
        QDir::Filters dirfilter = QDir::Dirs | QDir::NoDotAndDotDot;
        m_shareds = smb_dir.entryList(dirfilter);
    }

    QStringList var_libs_samba_dirs() {
        QDir smb_dir(var_lib_samba);
        QDir::Filters dirfilter = QDir::Dirs | QDir::NoDotAndDotDot;
        return smb_dir.entryList(dirfilter);
    }

    QStringList shareds() {
        return m_shareds;
    }

    QStringList usershares() {
        if (var_libs_samba_dirs().contains("usershares")) {
            QDir smb_dir_usershares(var_lib_samba_usersahres);
            QDir::Filters filefilter = QDir::Files;
            return smb_dir_usershares.entryList(filefilter);
        }
        return QStringList();
    }

    bool create(SambaShare share) {
        if (usershares().contains(share.sharename())) 
            return false;
        
        QFile share_file(var_lib_samba_usersahres + share.sharename());
        if (share_file.open(QFile::WriteOnly)) {
            share_file.write(share.content().toUtf8());
            share_file.close();
            return true;
        }
        return false;
    }

    bool remove(const QString &sharename) {
        if (usershares().contains(sharename) == false)
            return true;

        QFile share_file(var_lib_samba_usersahres + sharename);
        return share_file.remove();
    }

private:
    QStringList m_shareds;
    QStringList m_usershares;

};

#endif  //!__SAMBA_MANAGER__H__
